﻿#region Licence
// <copyright file="Core/App.xaml.cs" company="Kopigi">
// Copyright © Kopigi 2014
// </copyright>
// ****************************************************************************
// <author>Marc PLESSIS</author>
// <date>11/10/2014</date>
// <project>Nentang.wpf</project>
// <web>http://nentang.codeplex.com</web>
// <license>
// The MIT License (MIT)
// 
// Copyright (c) 2013 Marc Plessis
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// </license>
#endregion
#region using

using System;
using System.Windows;
using Core.Class;
using Core.Class.Mef;
using Core.Enums;
using Core.Helpers;
using Core.ViewModel;
using GalaSoft.MvvmLight.Ioc;
using Helpers;
using Interfaces;

#endregion

namespace Core
{
    /// <summary>
    /// Logique d'interaction pour App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 
        /// </summary>
        public App()
        {
            //IMPORTANT : Garder ce chargement ici car sinon avec le splashscreen le viewmodel locator se lance trop tard
            //Il faut également garder le chargement des dictionary sous peine de perdre l'ajout du locator si l'on met les dictionary en XAML

            //Ajout du ViewModelLocator à l'application courante
            Application.Current.Resources.Add("Locator", new ViewModelLocator());

            //Ajout des dictionary
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("ViewsTemplates.xaml", UriKind.RelativeOrAbsolute)
            });

            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Converters;component/Converters.xaml", UriKind.RelativeOrAbsolute)
            });

            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/UI;component/Params.xaml", UriKind.RelativeOrAbsolute)
            });

            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/UI;component/CommonsColors.xaml", UriKind.RelativeOrAbsolute)
            });

            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/UI;component/Generic.xaml", UriKind.RelativeOrAbsolute)
            });

            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/UI;component/Controls.xaml", UriKind.RelativeOrAbsolute)
            });

            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Themes/Generic.xaml", UriKind.RelativeOrAbsolute)
            });
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            //Composition MEF
            var bootstrapper = new BootStrapperMef(this);

            //Choix du théme de l'application
            ThemeManager.InitializeThemes();
            var themeUser = Core.Properties.Settings.Default.ThemeActual;
            if (themeUser < 0)
            {
                themeUser = 0;
            }
            ThemeManager.SetTheme((ThemesEnum)themeUser);

            //Choix de la couleur d'accentuation de l'application
            ThemeManager.InitializeAccentColors();
            var accentColorUser = Core.Properties.Settings.Default.AccentColorActual;
            if (string.IsNullOrEmpty(accentColorUser))
            {
                //Par défaut on prend la couleur bleue
                accentColorUser = "#FF71b1d1";
            }
            ThemeManager.SetAccentColor(ColorsHelper.HexToColor(accentColorUser));

            CurrentParametersInRunning.IsNetworkAvailable = NetworkStatus.IsAvailable;
            CurrentParametersInRunning.IsSqlServerOnline = CurrentParametersInRunning.IsNetworkAvailable && NetworkHelper.SetStatusSqlServer();
        }

        private void ApplicationDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            //On récupérer l'erreur puis on averti l'utilisateur est enfin on envoi un message d'erreur par mail
            SimpleIoc.Default.GetInstance<IServiceLogger>().LogFatalError(e.Exception.Message + "\\n" + e.Exception.InnerException + "\\n" + e.Exception.StackTrace, e.Exception);

            e.Handled = true;
        }
    }
}
